package com.github.umgr.controller;

import com.github.mvc.basic.controller.BaseController;
import com.github.umgr.pojo.entity.UserEntity;
import com.github.umgr.service.UserService;
import com.github.websocket.msg.Msg;
import com.github.websocket.server.WSServer;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    private static Logger logger = Logger.getLogger(UserController.class.getName());

    @Autowired
    private UserService userService;

    @RequestMapping(name = "用户主页", method = RequestMethod.GET, value = "/index")
    public String userIndex() {

        UserEntity nuser = new UserEntity();
        nuser.setUserName("root");
        nuser.setPassword("root");
        userService.insert(nuser);
        logger.debug("welcome to index !");
        return "index";
    }

    @RequestMapping(name = "获取用户", method = RequestMethod.GET, value = "/{userId}")
    @ResponseBody
    public Object getUser(@PathVariable String userId) {
        Msg msg = new Msg(null, null, null, "what is msg?", new Date(), 1);
        WSServer.pushBySys(msg);
        return ajaxOk("", userService.getById(userId));
    }

    @RequestMapping(name = "修改用户名", method = RequestMethod.PUT, value = "/{userId}", produces = "application/json")
    @ResponseBody
    public Object updateUserName(@RequestBody String param, @PathVariable String userId) {
        System.out.println(String.format("modify %s user name is %s ", userId, param));
        return ajaxOk(null, param);
    }
}
